<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-filter label="筛选分类">
				<cl-select :options="dict.get('DrugTypes')" prop="category" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索药品名称或生产商" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="drug-info" setup>
import { useCrud, useTable, useUpsert } from '@cool-vue/crud';
import { useCool } from '/@/cool';
import { reactive } from 'vue';
import { useDict } from '/$/dict';
const { dict } = useDict();
const { service } = useCool();

// 选项
const options = reactive({
	category: [
		{ label: '处方药', value: 0, type: 'danger' },
		{ label: '非处方药', value: 1, type: 'success' }
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: '名称',
			prop: 'name',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '类型',
			prop: 'type',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '规格',
			prop: 'specification',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '剂量',
			prop: 'dosage',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '生产商',
			prop: 'manufacturer',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '有效期',
			prop: 'expirationDate',
			component: {
				name: 'el-date-picker',
				props: { type: 'date', valueFormat: 'YYYY-MM-DD' }
			},
			required: true
		},
		{
			label: '库存',
			prop: 'stock',
			hook: 'number',
			component: { name: 'el-input-number' },
			required: true
		},
		{
			label: '价格',
			prop: 'price',
			hook: 'number',
			component: { name: 'el-input-number' },
			required: true
		},
		{
			label: '分类',
			prop: 'category',
			component: { name: 'el-radio-group', options: dict.get('DrugTypes') },
			value: 0,
			required: true
		},
		{
			label: '备注',
			prop: 'remark',
			component: { name: 'el-input', props: { type: 'textarea', rows: 4 } }
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: 'selection' },
		{ label: '名称', prop: 'name', minWidth: 140 },
		{ label: '类型', prop: 'type', minWidth: 140 },
		{ label: '规格', prop: 'specification', minWidth: 140 },
		{ label: '剂量', prop: 'dosage', minWidth: 140 },
		{ label: '生产商', prop: 'manufacturer', minWidth: 140 },
		{
			label: '有效期',
			prop: 'expirationDate',
			minWidth: 140,
			sortable: 'custom',
			component: { name: 'cl-date-text' }
		},
		{ label: '库存', prop: 'stock', minWidth: 140 },
		{ label: '价格', prop: 'price', minWidth: 140 },
		{ label: '分类', prop: 'category', dict: dict.get('DrugTypes'), minWidth: 120 },
		{ label: '备注', prop: 'remark', showOverflowTooltip: true, minWidth: 200 },
		{
			label: '创建时间',
			prop: 'createTime',
			minWidth: 170,
			sortable: 'desc',
			component: { name: 'cl-date-text' }
		},
		{
			label: '更新时间',
			prop: 'updateTime',
			minWidth: 170,
			sortable: 'custom',
			component: { name: 'cl-date-text' }
		},
		{ type: 'op', buttons: ['edit', 'delete'] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.drug.info
	},
	app => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
